
#include "user_init.h"


void RCC_PLL_ConfigDomain_SYS(uint32_t Source, uint32_t PLL_R, uint32_t PLL_DB, uint32_t PLL_O)
{
    MODIFY_REG(RCC->PLLCR, RCC_PLLCR_DB_Msk | RCC_PLLCR_REFPRSC_Msk | RCC_PLLCR_OSEL_Msk | RCC_PLLCR_INSEL_Msk,
               ((PLL_DB - 1) << RCC_PLLCR_DB_Pos) | PLL_R | PLL_O | Source);
}

void SelRCHFToPLL(uint32_t rchf, uint32_t clock)
{
    uint32_t counter = 0;
    uint32_t readystatus = 0;
    uint32_t div = FL_RCC_PLL_PSC_DIV8;

    if(clock > 64) { return; }

    //RCHFInit(rchf);
	FL_RCC_RCHF_WriteTrimValue(RCHF8M_TRIM);
	FL_RCC_RCHF_SetFrequency(clock);

    switch(rchf)
    {
        case FL_RCC_RCHF_FREQUENCY_16MHZ:
            div = FL_RCC_PLL_PSC_DIV16;
            break;

        case FL_RCC_RCHF_FREQUENCY_24MHZ:
            div = FL_RCC_PLL_PSC_DIV24;
            break;

        default:
            break;
    }

    FL_FLASH_SetReadWait(FLASH, FL_FLASH_READ_WAIT_2CYCLE);

    RCC_PLL_ConfigDomain_SYS(FL_RCC_PLL_CLK_SOURCE_RCHF, div, clock, FL_RCC_PLL_OUTPUT_X1);

    FL_RCC_PLL_Enable();            //使能PLL
    do
    {
        readystatus = FL_RCC_IsActiveFlag_PLLReady();
        counter++;
    } while((readystatus != 0x1U) && (counter != PLL_TIMEOUT));       //等待PLL建立稳定，才能将系统时钟源切到PLL，如没有建立，则进入超时

    FL_RCC_SetAHBPrescaler(FL_RCC_AHBCLK_PSC_DIV1);
    FL_RCC_SetAPB1Prescaler(FL_RCC_APB1CLK_PSC_DIV1);
    FL_RCC_SetAPB2Prescaler(FL_RCC_APB2CLK_PSC_DIV1);
    FL_RCC_SetSystemClockSource(FL_RCC_SYSTEM_CLK_SOURCE_PLL);

    if(clock <= 24)
    {
        FL_FLASH_SetReadWait(FLASH, FL_FLASH_READ_WAIT_0CYCLE);
    }
    else if((clock > 24) && (clock <= 48))
    {
        FL_FLASH_SetReadWait(FLASH, FL_FLASH_READ_WAIT_1CYCLE);
    }
    else
    {
        FL_FLASH_SetReadWait(FLASH, FL_FLASH_READ_WAIT_2CYCLE);
    }

    SystemCoreClockUpdate();
}

void BaseTimeInit(void)
{
    FL_ATIM_InitTypeDef    TimerBaseInitStruct;

    TimerBaseInitStruct.clockSource = FL_RCC_ATIM_CLK_SOURCE_APB2CLK;
    TimerBaseInitStruct.prescaler = 64 - 1;
    TimerBaseInitStruct.counterMode = FL_ATIM_COUNTER_DIR_UP;
    TimerBaseInitStruct.autoReload = 5000;	// 5ms
    TimerBaseInitStruct.autoReloadState = FL_DISABLE;
    TimerBaseInitStruct.clockDivision = FL_ATIM_CLK_DIVISION_DIV1;
    TimerBaseInitStruct.repetitionCounter = 0;

    FL_ATIM_Init(ATIM, &TimerBaseInitStruct);
    FL_ATIM_ClearFlag_Update(ATIM);
    FL_ATIM_EnableIT_Update(ATIM);

	FL_NVIC_ConfigTypeDef    InterruptConfigStruct;

    InterruptConfigStruct.preemptPriority = 0x00;
    FL_NVIC_Init(&InterruptConfigStruct, ATIM_IRQn);

    //使能ATIM
    FL_ATIM_Enable(ATIM);

}


void GPIO_Init(void)
{
    FL_GPIO_InitTypeDef    GPIO_InitStruct;

    GPIO_InitStruct.outputType = FL_GPIO_OUTPUT_PUSHPULL;
    GPIO_InitStruct.pull       = FL_ENABLE;
    GPIO_InitStruct.remapPin   = FL_DISABLE;
	
	GPIO_InitStruct.mode       = FL_GPIO_MODE_INPUT;

	//SW1_1
	GPIO_InitStruct.pin        = FL_GPIO_PIN_8;
    FL_GPIO_Init(GPIOC, &GPIO_InitStruct);

	//SW1_2
	GPIO_InitStruct.pin 	   = FL_GPIO_PIN_9;
	FL_GPIO_Init(GPIOD, &GPIO_InitStruct);

	//SW1_3
	GPIO_InitStruct.pin        = FL_GPIO_PIN_9;
    FL_GPIO_Init(GPIOC, &GPIO_InitStruct);

	//SW1_4
	GPIO_InitStruct.pin        = FL_GPIO_PIN_10;
    FL_GPIO_Init(GPIOC, &GPIO_InitStruct);

	//SW2
	GPIO_InitStruct.pin        = FL_GPIO_PIN_10;
    FL_GPIO_Init(GPIOA, &GPIO_InitStruct);
	
	//SW3
	GPIO_InitStruct.pin        = FL_GPIO_PIN_2;
    FL_GPIO_Init(GPIOB, &GPIO_InitStruct);

	//SW4
	GPIO_InitStruct.pin        = FL_GPIO_PIN_3;
    FL_GPIO_Init(GPIOB, &GPIO_InitStruct);

	//SW5
	GPIO_InitStruct.pin        = FL_GPIO_PIN_4;
    FL_GPIO_Init(GPIOB, &GPIO_InitStruct);

	//SW6
	GPIO_InitStruct.pin        = FL_GPIO_PIN_5;
    FL_GPIO_Init(GPIOB, &GPIO_InitStruct);

	//SW7
	GPIO_InitStruct.pin        = FL_GPIO_PIN_8;
    FL_GPIO_Init(GPIOB, &GPIO_InitStruct);

	//SW8
	GPIO_InitStruct.pin        = FL_GPIO_PIN_9;
    FL_GPIO_Init(GPIOA, &GPIO_InitStruct);

	// 输出
	GPIO_InitStruct.mode       = FL_GPIO_MODE_OUTPUT;

	// LED_D
	GPIO_InitStruct.pin        = FL_GPIO_PIN_7;
    FL_GPIO_Init(GPIOA, &GPIO_InitStruct);

	// LED_W
	GPIO_InitStruct.pin        = FL_GPIO_PIN_8;
    FL_GPIO_Init(GPIOA, &GPIO_InitStruct);

	// DIN
	GPIO_InitStruct.pin        = FL_GPIO_PIN_10;
    FL_GPIO_Init(GPIOB, &GPIO_InitStruct);

	// SCLK 
	GPIO_InitStruct.pin        = FL_GPIO_PIN_9;
    FL_GPIO_Init(GPIOB, &GPIO_InitStruct);

}

void UART4_WifiInit(void)
{
	FL_GPIO_InitTypeDef    GPIO_InitStruct;

    FL_UART_InitTypeDef    defaultInitStruct;

    GPIO_InitStruct.pin = FL_GPIO_PIN_0;
    GPIO_InitStruct.mode = FL_GPIO_MODE_DIGITAL;
    GPIO_InitStruct.outputType = FL_GPIO_OUTPUT_PUSHPULL;
    GPIO_InitStruct.pull = FL_ENABLE;
    GPIO_InitStruct.remapPin = FL_DISABLE;
    FL_GPIO_Init(GPIOA, &GPIO_InitStruct);	// RX

    GPIO_InitStruct.pin        = FL_GPIO_PIN_1;
    GPIO_InitStruct.mode       = FL_GPIO_MODE_DIGITAL;
    GPIO_InitStruct.outputType = FL_GPIO_OUTPUT_PUSHPULL;
    GPIO_InitStruct.pull       = FL_DISABLE;
    GPIO_InitStruct.remapPin   = FL_DISABLE;
    FL_GPIO_Init(GPIOA, &GPIO_InitStruct);	// TX

    defaultInitStruct.clockSrc          = FL_RCC_UART2_CLK_SOURCE_APB1CLK;
    defaultInitStruct.baudRate          = 115200;
    defaultInitStruct.dataWidth         = FL_UART_DATA_WIDTH_8B;
    defaultInitStruct.stopBits          = FL_UART_STOP_BIT_WIDTH_1B;
    defaultInitStruct.parity            = FL_UART_PARITY_EVEN;
    defaultInitStruct.transferDirection = FL_UART_DIRECTION_TX_RX;

    FL_UART_Init(UART4, &defaultInitStruct);

	FL_NVIC_ConfigTypeDef    InterruptConfigStruct;

    InterruptConfigStruct.preemptPriority = 2;
    FL_NVIC_Init(&InterruptConfigStruct, UART4_IRQn);

    FL_UART_ClearFlag_RXBuffFull(UART4);
    FL_UART_EnableIT_RXBuffFull(UART4);           //使能接收中断
}

// 电机
void UART0_MOTOInit(void)
{
	FL_GPIO_InitTypeDef    GPIO_InitStruct;

    FL_UART_InitTypeDef    defaultInitStruct;

    GPIO_InitStruct.pin = FL_GPIO_PIN_2;
    GPIO_InitStruct.mode = FL_GPIO_MODE_DIGITAL;
    GPIO_InitStruct.outputType = FL_GPIO_OUTPUT_PUSHPULL;
    GPIO_InitStruct.pull = FL_ENABLE;
    GPIO_InitStruct.remapPin = FL_DISABLE;
    FL_GPIO_Init(GPIOA, &GPIO_InitStruct);	// RX

    GPIO_InitStruct.pin        = FL_GPIO_PIN_3;
    GPIO_InitStruct.mode       = FL_GPIO_MODE_DIGITAL;
    GPIO_InitStruct.outputType = FL_GPIO_OUTPUT_PUSHPULL;
    GPIO_InitStruct.pull       = FL_DISABLE;
    GPIO_InitStruct.remapPin   = FL_DISABLE;
    FL_GPIO_Init(GPIOA, &GPIO_InitStruct);	// TX

    defaultInitStruct.clockSrc          = FL_RCC_UART0_CLK_SOURCE_APB1CLK;
    defaultInitStruct.baudRate          = 115200;
    defaultInitStruct.dataWidth         = FL_UART_DATA_WIDTH_8B;
    defaultInitStruct.stopBits          = FL_UART_STOP_BIT_WIDTH_1B;
    defaultInitStruct.parity            = FL_UART_PARITY_EVEN;
    defaultInitStruct.transferDirection = FL_UART_DIRECTION_TX_RX;

    FL_UART_Init(UART0, &defaultInitStruct);

	FL_NVIC_ConfigTypeDef    InterruptConfigStruct;

    InterruptConfigStruct.preemptPriority = 2;
    FL_NVIC_Init(&InterruptConfigStruct, UART0_IRQn);

    FL_UART_ClearFlag_RXBuffFull(UART0);
    FL_UART_EnableIT_RXBuffFull(UART0);           //使能接收中断

}

// 上位机
void UART1_MONIInit(void)
{
	FL_GPIO_InitTypeDef    GPIO_InitStruct;

    FL_UART_InitTypeDef    defaultInitStruct;

    GPIO_InitStruct.pin = FL_GPIO_PIN_13;
    GPIO_InitStruct.mode = FL_GPIO_MODE_DIGITAL;
    GPIO_InitStruct.outputType = FL_GPIO_OUTPUT_PUSHPULL;
    GPIO_InitStruct.pull = FL_ENABLE;
    GPIO_InitStruct.remapPin = FL_DISABLE;
    FL_GPIO_Init(GPIOB, &GPIO_InitStruct);	// RX

    GPIO_InitStruct.pin        = FL_GPIO_PIN_14;
    GPIO_InitStruct.mode       = FL_GPIO_MODE_DIGITAL;
    GPIO_InitStruct.outputType = FL_GPIO_OUTPUT_PUSHPULL;
    GPIO_InitStruct.pull       = FL_DISABLE;
    GPIO_InitStruct.remapPin   = FL_DISABLE;
    FL_GPIO_Init(GPIOB, &GPIO_InitStruct);	// TX

    defaultInitStruct.clockSrc          = FL_RCC_UART1_CLK_SOURCE_APB1CLK;
    defaultInitStruct.baudRate          = 115200;
    defaultInitStruct.dataWidth         = FL_UART_DATA_WIDTH_8B;
    defaultInitStruct.stopBits          = FL_UART_STOP_BIT_WIDTH_1B;
    defaultInitStruct.parity            = FL_UART_PARITY_EVEN;
    defaultInitStruct.transferDirection = FL_UART_DIRECTION_TX_RX;

    FL_UART_Init(UART1, &defaultInitStruct);

	FL_NVIC_ConfigTypeDef    InterruptConfigStruct;

    InterruptConfigStruct.preemptPriority = 2;
    FL_NVIC_Init(&InterruptConfigStruct, UART1_IRQn);

    FL_UART_ClearFlag_RXBuffFull(UART1);
    FL_UART_EnableIT_RXBuffFull(UART1);           //使能接收中断

}

// PWM TIMER0 CH2  2KHz
void GPTIM0_PWMInit(void)
{

    FL_GPTIM_InitTypeDef    TimerBaseInitStruct;

    TimerBaseInitStruct.prescaler = 64 - 1;
    TimerBaseInitStruct.counterMode = FL_GPTIM_COUNTER_DIR_UP;
    TimerBaseInitStruct.autoReload = 500;	// 500us
    TimerBaseInitStruct.autoReloadState = FL_DISABLE;
    TimerBaseInitStruct.clockDivision = FL_GPTIM_CLK_DIVISION_DIV1;
    FL_GPTIM_Init(GPTIM0, &TimerBaseInitStruct);
    FL_GPTIM_EnableIT_Update(GPTIM0);

	// CH2
    FL_GPIO_InitTypeDef    GPIO_InitStruct;

    FL_GPTIM_OC_InitTypeDef    defaultInitStruct;

    GPIO_InitStruct.pin = FL_GPIO_PIN_11;
    GPIO_InitStruct.mode = FL_GPIO_MODE_DIGITAL;
    GPIO_InitStruct.outputType = FL_GPIO_OUTPUT_PUSHPULL;
    GPIO_InitStruct.pull = FL_DISABLE;
    GPIO_InitStruct.remapPin = FL_ENABLE;
    FL_GPIO_Init(GPIOB, &GPIO_InitStruct);

    defaultInitStruct.OCMode = FL_GPTIM_OC_MODE_PWM1;
    defaultInitStruct.OCFastMode = FL_DISABLE;
    defaultInitStruct.OCPreload = FL_DISABLE;
    defaultInitStruct.compareValue = 250;
    defaultInitStruct.OCPolarity = FL_GPTIM_OC_POLARITY_NORMAL;
    defaultInitStruct.OCETRFStatus = FL_DISABLE;
    FL_GPTIM_OC_Init(GPTIM0, FL_GPTIM_CHANNEL_2, &defaultInitStruct);

	// 使能GPTIM  	禁止输出
    FL_GPTIM_Enable(GPTIM0);
    FL_GPTIM_OC_DisableChannel(GPTIM0,FL_GPTIM_CHANNEL_2);

}

void IWDT_Init(void)
{
    FL_IWDT_InitTypeDef    IWDT_InitStruct;

    IWDT_InitStruct.overflowPeriod = FL_IWDT_PERIOD_2000MS;
    IWDT_InitStruct.iwdtWindows = 0;

    FL_IWDT_Init(IWDT, &IWDT_InitStruct);
}

void UserInit(void)
{
	SelRCHFToPLL(FL_RCC_RCHF_FREQUENCY_8MHZ, 64);
	BaseTimeInit();
	
	GPIO_Init();
	UART4_WifiInit();
	UART0_MOTOInit();
	UART1_MONIInit();
	GPTIM0_PWMInit();
#if(0 == DEBUG)
	IWDT_Init();
#endif
}

